package io.github.shenbinglife.springboot2.layui.admin.controller;

import io.github.shenbinglife.springboot2.layui.admin.entity.Dict;
import io.github.shenbinglife.springboot2.layui.admin.entity.Role;
import io.github.shenbinglife.springboot2.layui.admin.entity.User;
import io.github.shenbinglife.springboot2.layui.admin.service.impl.RoleService;
import io.github.shenbinglife.springboot2.layui.common.model.Result;
import io.github.shenbinglife.springboot2.layui.common.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("admin/roles")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 角色分页查询
     *
     * @param keyword
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("page")
    public Result<Page<Role>> getPageRoles(@RequestParam(required = false) String keyword,
                                           @RequestParam(defaultValue = "0") Integer page,
                                           @RequestParam(defaultValue = "20") Integer limit) {
        Page<Role> roles = roleService.page(keyword, page - 1, limit);
        return Result.success(roles);
    }

    /**
     * 角色查询
     *
     * @return
     */
    @GetMapping("dict")
    public Result<List<Dict>> getPageRoles(@RequestParam String scope) {
        List<Role> roles = roleService.getRoleByScope(scope);
        List<Dict> dicts = roles.stream()
                .map(Role::toDict)
                .collect(Collectors.toList());
        return Result.success(dicts);
    }


    /**
     * 根据ID查询角色
     *
     * @param roleId
     * @return
     */
    @GetMapping("{roleId}")
    public Result<Role> getRole(@PathVariable("roleId") Long roleId) {
        return roleService.getOptionalById(roleId)
                .map(Result::success)
                .orElse(Result.failure("角色不存在"));
    }

    /**
     * 查询角色的用户列表
     *
     * @param roleId
     * @return
     */
    @GetMapping("{roleId}/users")
    public Result<List<User>> getRoleUsers(@PathVariable("roleId") Long roleId) {
        List<User> users = roleService.getRoleUsers(roleId);
        return Result.success(users);
    }

    /**
     * 分页查询角色的用户列表
     *
     * @param roleId
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("{roleId}/users/page")
    public Result<Page<User>> getPageRoleUsers(@PathVariable("roleId") Long roleId,
                                               @RequestParam(required = false) String keyword,
                                               @RequestParam(defaultValue = "0") Integer page,
                                               @RequestParam(defaultValue = "20") Integer limit) {
        Page<User> users = roleService.getPageRoleUsers(roleId, keyword, page - 1, limit);
        return Result.success(users);
    }

    /**
     * 修改角色信息
     *
     * @param roleId
     * @param Role
     * @return
     */
    @PostMapping("{roleId}")
    public Result<Void> update(@PathVariable("roleId") Long roleId, @RequestBody Role Role) {
        Role.setId(roleId);
        roleService.update(Role);
        return Result.success();
    }

    /**
     * 修改角色状态
     *
     * @param roleId
     * @param enabled
     * @return
     */
    @PostMapping("{roleId}/enabled")
    public Result<Void> updateEnabled(@PathVariable("roleId") Long roleId, @RequestParam Boolean enabled) {
        roleService.updateEnabled(roleId, enabled);
        return Result.success();
    }

    /**
     * 创建角色
     *
     * @param Role
     * @return
     */
    @PutMapping
    public Result<Role> create(@RequestBody Role Role) {
        roleService.save(Role);
        return Result.success();
    }

    /**
     * 删除角色
     *
     * @param roleIds
     * @return
     */
    @DeleteMapping("{roleId}")
    public Result<Void> delete(@PathVariable("roleId") Long[] roleIds) {
        roleService.deleteById(roleIds);
        return Result.success();
    }

    /**
     * 添加用户到组织
     *
     * @param roleId
     * @param userIds
     * @return
     */
    @PostMapping("{roleId}/users")
    public Result<Void> addUsersToRole(@PathVariable("roleId") Long roleId, @RequestParam Long[] userIds) {
        roleService.addUsersToRole(roleId, userIds);
        return Result.success();
    }

    /**
     * 移除组织中的用户
     *
     * @param roleId
     * @param userIds
     * @return
     */
    @DeleteMapping("{roleId}/users/{userId}")
    public Result<Void> removeUsersFromRole(@PathVariable("roleId") Long roleId, @PathVariable("userId") Long[] userIds) {
        roleService.removeUsersFromRole(roleId, userIds);
        return Result.success();
    }


    @GetMapping("{roleId}/menus/tree")
    public Result<List<TreeNode<Long>>> getMenuTrees(@PathVariable("roleId") Long roleId) {
        List<TreeNode<Long>> tree = roleService.getMenuTrees(roleId);
        return Result.success(tree);
    }

    @PostMapping("{roleId}/menus")
    public Result<Void> addMenusToRole(@PathVariable("roleId") Long roleId, @RequestParam Long[] userIds) {
        roleService.saveRoleMenus(roleId, userIds);
        return Result.success();
    }
}
